package com.ruoyi.examination.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.examination.vo.ExaminationVO;
import com.ruoyi.system.service.IStoreroomService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.vo.StoreroomAndUserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.examination.domain.Examination;
import com.ruoyi.examination.service.IExaminationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参试设备Controller
 * 
 * @author mqs
 * @date 2024-05-03
 */
@Controller
@RequestMapping("/examination/examination")
public class ExaminationController extends BaseController
{
    private String prefix = "examination/examination";

    @Autowired
    private IExaminationService examinationService;

    @Autowired
    private IStoreroomService storeroomService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("examination:examination:view")
    @GetMapping()
    public String examination()
    {
        return prefix + "/examination";
    }

    /**
     * 查询参试设备列表
     */
    @RequiresPermissions("examination:examination:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExaminationVO examinationVO)
    {
        startPage();
        List<ExaminationVO> list = examinationService.selectExaminationList(examinationVO);
        return getDataTable(list);
    }

    /**
     * 导出参试设备列表
     */
    @RequiresPermissions("examination:examination:export")
    @Log(title = "参试设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExaminationVO examinationVO)
    {
        List<ExaminationVO> list = examinationService.selectExaminationList(examinationVO);
        ExcelUtil<ExaminationVO> util = new ExcelUtil<ExaminationVO>(ExaminationVO.class);
        return util.exportExcel(list, "参试设备数据");
    }

    /**
     * 新增参试设备
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<StoreroomAndUserVO> storerooms = storeroomService.selectStoreroom();
        model.addAttribute("storerooms",storerooms);
        return prefix + "/add";
    }

    /**
     * 新增保存参试设备
     */
    @RequiresPermissions("examination:examination:add")
    @Log(title = "参试设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Examination examination,String departmentName,String founderName)
    {
        SysUser sysUser = userService.selectUserByUserName(founderName);
        Long deptID = deptService.selectDeptIDDeptTree(departmentName);
        examination.setDepartmentId(deptID);
        examination.setFounder(sysUser.getUserId());
        return toAjax(examinationService.insertExamination(examination));
    }

    /**
     * 修改参试设备
     */
    @RequiresPermissions("examination:examination:edit")
    @GetMapping("/edit/{examinationId}")
    public String edit(@PathVariable("examinationId") Long examinationId, ModelMap mmap,Model model)
    {
        ExaminationVO examinationVO = examinationService.selectExaminationByExaminationId(examinationId);
        mmap.put("examination", examinationVO);
        List<StoreroomAndUserVO> storerooms = storeroomService.selectStoreroom();
        model.addAttribute("storerooms",storerooms);
        return prefix + "/edit";
    }

    /**
     * 修改保存参试设备
     */
    @RequiresPermissions("examination:examination:edit")
    @Log(title = "参试设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Examination examination,String deptName)
    {
        Long deptID = deptService.selectDeptIDDeptTree(deptName);
        examination.setDepartmentId(deptID);
        return toAjax(examinationService.updateExamination(examination));
    }

    /**
     * 查看参试设备
     */
    @RequiresPermissions("examination:examination:view")
    @GetMapping("/view/{examinationId}")
    public String view(@PathVariable("examinationId") Long examinationId, ModelMap mmap)
    {
        ExaminationVO examinationVO = examinationService.selectExaminationByExaminationId(examinationId);
        mmap.put("examination", examinationVO);
        return prefix + "/view";
    }

    /**
     * 删除参试设备
     */
    @RequiresPermissions("examination:examination:remove")
    @Log(title = "参试设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examinationService.deleteExaminationByExaminationIds(ids));
    }

    /**
     * 入场申请
     */
    @RequiresPermissions("examination:examination:entrance")
    @GetMapping("/entrance/{examinationId}")
    public String entrance(@PathVariable("examinationId") Long examinationId, ModelMap mmap,Model model)
    {
        ExaminationVO examinationVO = examinationService.selectExaminationByExaminationId(examinationId);
        mmap.put("examination", examinationVO);
        List<StoreroomAndUserVO> storerooms = storeroomService.selectStoreroom();
        model.addAttribute("storerooms",storerooms);
        return prefix + "/entrance";
    }
    /**
     * 入场保存参试设备
     */
    @RequiresPermissions("examination:examination:entrance")
    @Log(title = "参试设备", businessType = BusinessType.UPDATE)
    @PostMapping("/entrance")
    @ResponseBody
    public AjaxResult entrance(Examination examination,String deptName,String storeroomName)
    {
        Long deptID = deptService.selectDeptIDDeptTree(deptName);
        StoreroomAndUserVO storeroom = storeroomService.selectStoreroomByName(storeroomName);
        examination.setDepartmentId(deptID);
        examination.setStoreroomId(storeroom.getStoreroomId());
        examination.setExaminationStatus(1L);
        return toAjax(examinationService.entrance(examination));
    }

    /**
     * 离场
     */
    @RequiresPermissions("examination:examination:out")
    @Log(title = "参试设备", businessType = BusinessType.DELETE)
    @PostMapping( "/out")
    @ResponseBody
    public AjaxResult out(String ids)
    {
        return toAjax(examinationService.out(ids));
    }

    /**
     * 出库
     */
    @RequiresPermissions("examination:examination:outbound")
    @Log(title = "参试设备", businessType = BusinessType.DELETE)
    @PostMapping( "/outbound")
    @ResponseBody
    public AjaxResult outbound(String ids)
    {
        return toAjax(examinationService.outbound(ids));
    }

    /**
     * 入库
     */
    @RequiresPermissions("examination:examination:input")
    @Log(title = "参试设备", businessType = BusinessType.DELETE)
    @PostMapping( "/input")
    @ResponseBody
    public AjaxResult input(String ids)
    {
        return toAjax(examinationService.input(ids));
    }
}
